package com.ultrawise.survey.common.printfile;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.ultrawise.survey.common.utils.DateUtils;
import com.ultrawise.survey.common.utils.WebPath;
import com.ultrawise.survey.contants.SurveyConstants;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * 
 * 模板工具类
 */
public class FreeMarkertUtil {
	/**
	 * @param templatePath
	 *            模板文件存放目录
	 * @param templateName
	 *            模板文件名称
	 * @param root
	 *            数据模型根对象
	 * @param templateEncoding
	 *            模板文件的编码方式
	 * @param out
	 *            输出流
	 */
	public static void processTemplate(String templatePath, String templateName, String templateEncoding,
			Map<?, ?> root, Writer out) {
		try {
			Configuration config = new Configuration();
			File file = new File(templatePath);
			// 设置要解析的模板所在的目录，并加载模板文件
			config.setDirectoryForTemplateLoading(file);
			// 设置包装器，并将对象包装为数据模型
			config.setObjectWrapper(new DefaultObjectWrapper());

			// 获取模板,并设置编码方式，这个编码必须要与页面中的编码格式一致
			Template template = config.getTemplate(templateName, templateEncoding);
			// 合并数据模型与模板

			template.process(root, out);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} finally {
			try {
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	public static void writehtml(Template t, Map<String, Object> map, String outfile) {
		FileOutputStream outFile = null;
		Writer out = null;
		ByteArrayOutputStream ostream = null;
		try {

			// 输出文档信息
			ostream = new ByteArrayOutputStream();

			outFile = new FileOutputStream(outfile);
			out = new BufferedWriter(new OutputStreamWriter(outFile, "utf-8"));

			if (map == null) {
				map = new HashMap<String, Object>();
			}
			//???
			t.process(map, out);

		} catch (IOException e) {
			e.printStackTrace();

		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			try {
				out.close();
				ostream.close();
				outFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String getNewHtml(String filename, Map<String, Object> root, Integer syid) throws Exception {
		Configuration cfg = new Configuration();
		cfg.setDefaultEncoding("utf-8");
		cfg.setEncoding(Locale.getDefault(), "utf-8");
		Template t = null;
		String outfile = WebPath.getTomcatWebappsApppath() + SurveyConstants.SURVEY_OUTFILE_PATH;
		// 模版路径
		cfg.setDirectoryForTemplateLoading(new File(WebPath.getTomcatWebappsApppath()));
		// 模版路径
		try {
			t = cfg.getTemplate(SurveyConstants.SURVEY_TEMPLATE_PATH + File.separator + filename, "UTF-8");
		} catch (Exception e) {
			// throw e;
			filename = (String) SurveyConstants.SURVEY_TEMPLATE.get("ONE");
			t = cfg.getTemplate(SurveyConstants.SURVEY_TEMPLATE_PATH + File.separator + filename, "UTF-8");
		}

		t.setEncoding("utf-8");
		// Map<String, Object> root = new HashMap<String, Object>();
		// root.put("message", "大家好，这是freemark");
		// root.put("fname", "http://www.ultrawise.com.cn/");
		// TSurveyInfoBase tb= new TSurveyInfoBase();
		// tb.setSy_name("DDDDDDDDDDDDD大家好");
		// root.put("base",tb);
		String outfilename = "SY-" + syid + "-" + DateUtils.getDate("yyyyMMddHHmmss")
				+ SurveyConstants.SURVEY_TEMPLATEFILE_SUFFIX;

		FreeMarkertUtil.writehtml(t, root, outfile + outfilename);
		return SurveyConstants.SURVEY_OUTFILE_PATH + outfilename;
	}

	
	public static String getNewViewHtml(String filename, Map<String, Object> root, Integer syid,String outfilename) throws Exception {
		Configuration cfg = new Configuration();
		cfg.setDefaultEncoding("utf-8");
		cfg.setEncoding(Locale.getDefault(), "utf-8");
		Template t = null;
		String outfile = WebPath.getTomcatWebappsApppath() + SurveyConstants.SURVEY_OUTFILE_PATH;
		// 模版路径
		cfg.setDirectoryForTemplateLoading(new File(WebPath.getTomcatWebappsApppath()));
		// 模版路径
		try {
			t = cfg.getTemplate(SurveyConstants.SURVEY_TEMPLATE_PATH + File.separator + filename, "UTF-8");
		} catch (Exception e) {
			// throw e;
			filename = (String) SurveyConstants.SURVEY_TEMPLATE.get("ONE");
			t = cfg.getTemplate(SurveyConstants.SURVEY_TEMPLATE_PATH + File.separator + filename, "UTF-8");
		}

		t.setEncoding("utf-8");
		// Map<String, Object> root = new HashMap<String, Object>();
		// root.put("message", "大家好，这是freemark");
		// root.put("fname", "http://www.ultrawise.com.cn/");
		// TSurveyInfoBase tb= new TSurveyInfoBase();
		// tb.setSy_name("DDDDDDDDDDDDD大家好");
		// root.put("base",tb);
		 outfilename = outfilename + SurveyConstants.SURVEY_TEMPLATEFILE_SUFFIX;

		FreeMarkertUtil.writehtml(t, root, outfile + outfilename);
		return SurveyConstants.SURVEY_OUTFILE_PATH + outfilename;
	}

	
	public static void main(String[] args) throws Exception {
		// FreeMarkertUtil.getNewHtml("test.ftl",null "AAAA");
	}

}